package br.com.wepa.webapps.orca.controle.servlets;

import java.util.List;

import javax.servlet.http.HttpSession;

import br.com.wepa.webapps.logger.TraceLogger;
import br.com.wepa.webapps.orca.controle.ConstantesControle;
import br.com.wepa.webapps.orca.controle.ControleWeb;
import br.com.wepa.webapps.orca.controle.exception.SessaoExpiradaException;
import br.com.wepa.webapps.orca.logica.negocio.BusinessConstants;
import br.com.wepa.webapps.orca.logica.negocio.facade.FacadeDelegator;
import br.com.wepa.webapps.orca.logica.negocio.facade.LoginFacadeHome;
import br.com.wepa.webapps.orca.logica.negocio.facade.to.LoginTO;
import br.com.wepa.webapps.orca.visual.client.MenuService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class MenuServiceImpl extends RemoteServiceServlet implements
		MenuService,ConstantesControle {
    /**
     * Objeto de Log
     */
    @SuppressWarnings("unused")
	private static final TraceLogger logger  = new TraceLogger(
    		MenuServiceImpl.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = -4720558853274641932L;

	public List getMenu() {		
		List retorno = null;
		try {
			ControleWeb.setCurrents(getThreadLocalRequest(),getThreadLocalResponse());
			
			LoginFacadeHome facade = FacadeDelegator.getLoginFacade();
			 
			HttpSession session = ControleWeb.getSessaoCorrente();
			Object menu = session.getAttribute(MENU);
			if(menu == null){
				LoginTO to = facade.instanceTO();
				to.setContextPath(getThreadLocalRequest().getContextPath());	
				to.setUsuarioSessao(ControleWeb.getUsuarioSessaoCorrente());
				
				retorno = FacadeDelegator.getLoginFacade().loadMenuItens(to);	
				session.setAttribute(MENU,retorno);
			}else{
				retorno = (List) menu;
			}
			
		}catch (SessaoExpiradaException e) {
			// faz nada
		} 
		catch (Exception e) {
			throw new RuntimeException(BusinessConstants.ERROR_MONTAGEM_MENU, e);
		}
		return retorno;

	}

}
